#include "widget.h"
#include "gameplay.h"
#include "qobjectdefs.h"
#include "settingsdialog.h"
#include"instructionwidget.h"
#include<qgraphicsview.h>
#include<QTimer>
#include<QKeyEvent>
#include<QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    // game.gameview.setFocusPolicy(Qt::StrongFocus);

    game.setFixedSize(500,280);

    // 创建主界面和按钮
    maininterface.setParent(this);
    QPixmap p1=QPixmap("3.png");
    maininterface.setPixmap(p1);
    this->setFixedSize(500,280);
    start .setText("开始");
    start.setParent(this);
    instruct.setText("介绍");
    instruct.setParent(this);
    setting.setText("设置");
    setting.setParent(this);
    title.setText("Tom And Jerry");
    title.setParent(this);
    ctom .setText("Tom");
    ctom.setParent(this);
    cjerry.setText("Jerry");
    cjerry.setParent(this);

    t.setPixmap(QPixmap("Tom"));
    t.setParent(this);
    j.setPixmap(QPixmap("Jerry"));
    j.setParent(this);

    t.move(40,110);
    j.move(180,110);

    // 设置按钮大小和位置
    title.setFixedSize(250,50);
    start.setFixedSize(80, 30);
    instruct.setFixedSize(80, 30);
    setting.setFixedSize(80, 30);
    ctom.setFixedSize(80, 30);
    cjerry.setFixedSize(80, 30);
    int x = this->width(), y = this->height();

    title.move(10,10);
    start.move((x-100), y * 0.25);
    instruct.move((x-100), y * 0.5);
    setting.move((x-100), y*0.75);
    ctom.move(40,230);
    cjerry.move(180,230);


    connect(&ctom,SIGNAL(clicked()),this,SLOT(message1()));
    connect(&ctom,&QAbstractButton::clicked,&game,&gamePlay::chtom);
    connect(&cjerry,SIGNAL(clicked()),this,SLOT(message2()));
    connect(&cjerry,&QAbstractButton::clicked,&game,&gamePlay::chjerry);


    //按钮样式
    title.setStyleSheet   ("background-color: transparent; border: none;font-family: 华文彩云; font-size: 40px; color: #F33333;");
    start.setStyleSheet   ("background-color: transparent; border: none;font-family: 华文琥珀; font-size: 24px; color: #F28034;");
    instruct.setStyleSheet("background-color: transparent; border: none;font-family: 华文琥珀; font-size: 24px; color: #F20004;");
    setting.setStyleSheet ("background-color: transparent; border: none;font-family: 华文琥珀; font-size: 24px; color: #00F2DC;");
    ctom.setStyleSheet    ("background-color: blue       ; border: none;font-family: 华文琥珀; font-size: 24px; color: #89FF7C;");
    cjerry.setStyleSheet  ("background-color: brown      ; border: none;font-family: 华文琥珀; font-size: 24px; color: #FF8737;");


    // 连接设置按钮的点击信号到槽函数
    connect(&setting, SIGNAL(clicked()),this,SLOT(showSettingsDialog()));

    connect(&instruct,SIGNAL(clicked()),this,SLOT(gotoroleinstruction()));

    connect(&start,SIGNAL(clicked()),this,SLOT(gotogameplay()));




    // 设置布局
    QVBoxLayout mainbox;
    mainbox.addWidget(&maininterface);
    this->setLayout(&mainbox);


}
void Widget::gotoroleinstruction()
{
    // 创建并显示 InstructionWidget 对象
    instructionwidget *instructionWidget = new instructionwidget;
    instructionWidget->show();
}

// 槽函数，显示设置对话框
void Widget::showSettingsDialog()
{
    // 创建并显示设置对话框
    SettingsDialog *settingsDialog = new SettingsDialog(this);
    settingsDialog->exec(); // 使用 exec() 函数以模态方式显示对话框
}

void Widget:: gotogameplay(){

    game.speed=1;
    game.temp.setPos(100,100);
    game.show();
};



Widget::~Widget() {}
